<?php

declare(strict_types=1);

namespace App\Traits\Model\Orders;


use App\Constants\Order\OrderCode;
use App\Model\Orders\Order as OrderModel;
use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Relations\BelongsTo;

/**
 * Trait Invoice
 * @author zx
 * @date 2023-11-02
 * @package App\Traits\Model\Orders
 */
trait Invoice
{
    /**
     * 关联订单
     * @return BelongsTo
     */
    public function order():BelongsTo
    {
        return $this->belongsTo(OrderModel::class);
    }

    /**
     * 新增后操作
     * @param Created $created
     */
    public function created(Created $created)
    {
        /** @var static $model */
        $model = $created->getModel();
        /** @var OrderModel $orderModel */
        $orderModel = $model->order;
        // 订单操作发货
        if ($orderModel->status === OrderCode::STATUS_PENDING_DELIVERY) {
            $orderModel->fill([
                'status' => OrderCode::STATUS_PENDING_RECEIVED,
                'shipping_at' => date('Y-m-d H:i:s')
            ])->save();
        }
    }
}
